/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package csc668868webappteamsp12aproject1;

import java.util.Calendar;
import java.text.SimpleDateFormat;

/**
 * Parses a line from transactions.txt into strings which are converted
 * (if necessary) and stored in a Transaction object.
 * inputs: filename.txt
 * output: Transaction object or null if reached EOF
 * @author Jeff Hung
 */
public class TransactionReader {
    private boolean atEOF = false;
    private char ch; // next character to process
    private int ascii_ch;
    private SourceReader source = null;
    private Transaction thisTransaction;
    private LineItem thisLineItem;
    private Payment thisPayment;
    private String paymentType;
    
    static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
/**
 *  Construct a new TransactionReader
 */    
    public TransactionReader(String sourceFile) throws Exception {
        source = new SourceReader(sourceFile);
        ch = source.read();
    }    

    public Transaction getNextTransaction() {
        if(atEOF) {
            if(source!=null) {
                source.close();
                source = null;
            }
            return null;
        }
        String temp = "";
        thisTransaction = new Transaction();
        //make timestamp
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
        String temp2 = sdf.format(cal.getTime());
        thisTransaction.setTimestamp(temp2);
        try {
            //get name
            int currentLineno = source.getLineno();
            while(currentLineno == source.getLineno()) {
                thisTransaction.setName(temp);
                temp = temp + ch;
                ch = source.read();
            }                        
            
            //get line items
            boolean hasLineItem = true;
            while(hasLineItem){
                temp = "";
                ascii_ch = (int) ch;
                if((ascii_ch >= 48)&&(ascii_ch <= 57)) { //check if line item
                    thisLineItem = new LineItem();
                    //get upc for line item
                    for(int i=1; i<5; i++) {
                        temp = temp + ch;
                        thisLineItem.setUPC(temp);    
                        ch = source.read();
                    }

                    currentLineno = source.getLineno();
                    ch = source.read();
                    if (currentLineno != source.getLineno()) {
                        thisLineItem.setQuantity(1);
                    } else {
                        //throw away spaces
                        temp = "";
                        for(int i=6; i<10; i++) {
                            ch = source.read();
                        }                    
                        //get quantity for line item
                        currentLineno = source.getLineno();
                        while(currentLineno == source.getLineno()) {
                            ascii_ch = (int) ch;
                            if (ascii_ch == 32) {
                                int quantity = Integer.parseInt(temp);
                                thisLineItem.setQuantity(quantity);
                            } else {
                                temp = temp + ch;
                            }
                            ch = source.read();
                        }
                    }
                    //add line item to transaction
                    thisTransaction.addLineItem(thisLineItem);
                    
                } else {
                    hasLineItem = false;
                }
             } //end while for get line items
            
            //get payment
            thisPayment = new Payment();
            ascii_ch = (int) ch;
            while(ascii_ch != 67) {
                ch = source.read(); //advance to first capital C
                ascii_ch = (int) ch;
            }
            ch = source.read();
            ascii_ch = (int) ch;
            if (ascii_ch == 65) {
                paymentType = "CASH/CHECK";
                thisPayment.setType(paymentType);
                thisPayment.setCardNumber("");
            } else if (ascii_ch == 82) {
                paymentType = "CREDIT";
                thisPayment.setType(paymentType);
                thisPayment.setValue("");
            }
            while((ascii_ch < 48)||(ascii_ch > 57)) {
                ch = source.read(); //advance to first number
                ascii_ch = (int) ch;
            }
            temp = "";
            currentLineno = source.getLineno();
            while(currentLineno == source.getLineno()) {
                if (paymentType.equals("CASH/CHECK")) {
                    thisPayment.setValue(temp);
                    thisTransaction.setPayment(thisPayment);
                } else if (paymentType.equals("CREDIT")) {
                    thisPayment.setCardNumber(temp);
                    thisTransaction.setPayment(thisPayment);
                }
                temp = temp + ch;
                ch = source.read();
            }
            
            //skip blank line
            currentLineno = source.getLineno();
            while(currentLineno == source.getLineno()) {
                ch = source.read();
            }            
        } catch (Exception e) {
            atEOF = true;
        }

        return thisTransaction;
    }    
    
}
